﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Collections.ObjectModel;
using System.ComponentModel;
using Prism.Core;

namespace GameFrameLibrary
{
    /// <summary>
    /// Interaction logic for DataBox.xaml
    /// </summary>
    public partial class DataBox : UserControl
    {
        public DataBox()
        {
            InitializeComponent();
            this.Items = new Collection<IPostionMarked>();
            for (var i = 0; i < 100; i++)
            {
                var obj = new NullObject() { Postion = i };
                obj.ShotCut = new BitmapImage(new Uri("Frames/Images/NullObject.png", UriKind.Relative));
                this.Items.Add(obj);

            }
            this.Contents = new ObservableCollection<TabItem>();
            for (var i = 0; i < 10; i++)
            {
                var listBox = new ListBox();
                ScrollViewer.SetHorizontalScrollBarVisibility(listBox, ScrollBarVisibility.Disabled);
                ScrollViewer.SetVerticalScrollBarVisibility(listBox, ScrollBarVisibility.Disabled);
                listBox.ItemTemplate = this.Resources["ListBoxDataTemplate"] as DataTemplate;
                var tabItem = new TabItem();
                tabItem.Content = listBox;
                var textBlockStyle = this.FindResource("TextBlockStyle") as Style;

                var textBlock = new TextBlock() { Text = i.ToString(), Style = textBlockStyle, FontSize = 24 };
                textBlock.MouseEnter += new MouseEventHandler(textBlock_MouseEnter);
                tabItem.Header = textBlock;
                this.Contents.Add(tabItem);
            }
            this.myTabControl.SelectedIndex = 0;
        }

        void textBlock_MouseEnter(object sender, MouseEventArgs e)
        {
            Prism.Games.GalEngine.Scripts.Parser.Input.GameCommands.ButtonMouseEnter.Execute(null, sender as IInputElement);
        }

        protected Collection<IPostionMarked> Items { get; set; }

        protected ObservableCollection<TabItem> Contents { get; set; }

        public virtual object SelectedItem
        {
            get
            {
                return (this.myTabControl.SelectedContent as ListBox).SelectedItem;
            }
        }

        public virtual void DistributeElement(IEnumerable<IPostionMarked> elements)
        {
            foreach (var i in elements)
            {
                if (i.Postion > 99)
                {
                    return;
                }
                if (i.Postion < 0)
                {
                    i.Postion = 0;
                }
                this.Items[i.Postion] = i;
            }
            //
            this.myTabControl.ItemsSource = this.Contents;

            for (var i = 0; i < 10; i++)
            {
                var listBox = (this.Contents[i] as TabItem).Content as ListBox;
                listBox.Items.Clear();
                for (var j = i * 10; j < (i + 1) * 10; j++)
                {
                    listBox.Items.Add(this.Items[j]);
                }
            }
        }

        private void Button_MouseEnter(object sender, MouseEventArgs e)
        {
            Prism.Games.GalEngine.Scripts.Parser.Input.GameCommands.ButtonMouseEnter.Execute(null, sender as IInputElement);
        }
    }

    public class NullObject : INotifyPropertyChanged, IPostionMarked
    {
        private int postion;
        public int Postion
        {
            get { return this.postion; }
            set { this.postion = value; this.OnPropertyChanged("Postion"); }
        }

        private BitmapSource shotCut;

        public BitmapSource ShotCut
        {
            get { return shotCut; }
            set { shotCut = value; this.OnPropertyChanged("ShotCut"); }
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// When a property changes.
        /// </summary>
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// The PropertyChanged event is raised.
        /// </summary>
        /// <param name="e">The event contains the parameter.</param>
        protected void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, e);
            }
        }

        /// <summary>
        /// The PropertyChanged event is raised.
        /// </summary>
        /// <param name="propertyName">Change the name of the attribute.</param>
        protected void OnPropertyChanged(string propertyName)
        {
            this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
